//
//  SubvistaPicoYPlaca.m
//  Pico y placa
//
//  Created by Mario Franco on 12/12/12.
//  Copyright (c) 2012 Mario Franco. All rights reserved.
//

#import "SubvistaPicoYPlaca.h"
#import "Regla.h"
#import "Horario.h"

@interface SubvistaPicoYPlaca ()

@end

@implementation SubvistaPicoYPlaca
@synthesize tipoVehiculo;


- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
	// Do any additional setup after loading the view.
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

-(void)pintarPlacaIPhone
{
    UIView *subvista = [[UIView alloc] initWithFrame:CGRectMake(0, 7, 320, 349)];
    
    //Imagen de fondo (0)
    CGRect imagenRect = CGRectMake(7, 10, 307, 329);
    UIImageView *imagen = [[UIImageView alloc] initWithFrame:imagenRect];    
    [subvista addSubview:imagen];
    
    //Tiene vigencia
    if([self calcularPosicionDeHoy] != -1)
    {        
        //Imagen de fondo (0)
        imagen.image = [UIImage imageNamed:@"placa2.png"];
        
        //Label Ciudad (1)
        CGRect lblCiudadRect = CGRectMake(90, 34, 210, 30);
        UILabel *lblCiudad = [[UILabel alloc] initWithFrame:lblCiudadRect];
        lblCiudad.text = tipoVehiculo.ciudad.nombre;
        [lblCiudad setBackgroundColor:[UIColor clearColor]];
        lblCiudad.textAlignment = NSTextAlignmentCenter;
        [lblCiudad setFont:[UIFont fontWithName:@"Arial-BoldMT" size:28.0]];
        [subvista addSubview:lblCiudad];
        
        //Imagen del vehículo (2)
        CGRect imagen2Rect = CGRectMake(32, 53, 63, 51);
        UIImageView *imagen2 = [[UIImageView alloc] initWithFrame:imagen2Rect];
        if([tipoVehiculo.tipo isEqualToString:@"Particular"])
            imagen2.image = [UIImage imageNamed:@"car.png"];
        else if([tipoVehiculo.tipo rangeOfString:@"Moto"].location != NSNotFound)
            imagen2.image = [UIImage imageNamed:@"moto.png"];
        else if([tipoVehiculo.tipo isEqualToString:@"Taxi"])
            imagen2.image = [UIImage imageNamed:@"taxi.png"];
        else if([tipoVehiculo.tipo isEqualToString:@"Bus"])
            imagen2.image = [UIImage imageNamed:@"bus.png"];
        [subvista addSubview:imagen2];
        
        //Label TipoVehículo (3)
        CGRect lblTipoRect = CGRectMake(32, 85, 63, 50);
        UILabel *lblTipo = [[UILabel alloc] initWithFrame:lblTipoRect];
        lblTipo.text = tipoVehiculo.tipo;
        lblTipo.numberOfLines = 2;
        [lblTipo setBackgroundColor:[UIColor clearColor]];
        lblTipo.textAlignment = NSTextAlignmentCenter;
        [lblTipo setFont:[UIFont fontWithName:@"Arial-BoldMT" size:12.0]];
        [subvista addSubview:lblTipo];
        
        //Imagen highlight (4)
        CGRect imagen3Rect = CGRectMake(14, 152, 293, 23);
        UIImageView *imagen3 = [[UIImageView alloc] initWithFrame:imagen3Rect];
        imagen3.image = [UIImage imageNamed:@"highlight.png"];
        [subvista addSubview:imagen3];
        
        //Label Horario 1 (5)
        UILabel *lblHorario1 = [[UILabel alloc] init];
        [lblHorario1 setBackgroundColor:[UIColor clearColor]];
        lblHorario1.textAlignment = NSTextAlignmentCenter;
        [subvista addSubview:lblHorario1];
        
        //Label Horario 2 (6)
        UILabel *lblHorario2 = [[UILabel alloc] init];
        [lblHorario2 setBackgroundColor:[UIColor clearColor]];
        lblHorario2.textAlignment = NSTextAlignmentCenter;
        [subvista addSubview:lblHorario2];
        
        //Label Horario 3 (7)
        UILabel *lblHorario3 = [[UILabel alloc] init];
        [lblHorario3 setBackgroundColor:[UIColor clearColor]];
        lblHorario3.textAlignment = NSTextAlignmentCenter;
        [subvista addSubview:lblHorario3];
        
        //Label Vigencia1 (8)
        CGRect lblVigencia1Rect = CGRectMake(86, 300, 85, 35);
        UILabel *lblVigencia1 = [[UILabel alloc] initWithFrame:lblVigencia1Rect];
        lblVigencia1.text = @"Vigente hasta:";
        [lblVigencia1 setBackgroundColor:[UIColor clearColor]];
        lblVigencia1.textAlignment = NSTextAlignmentLeft;
        [lblVigencia1 setFont:[UIFont fontWithName:@"Arial-BoldMT" size:12.0]];
        [subvista addSubview:lblVigencia1];
        
        //Label Vigencia2 (9)
        CGRect lblVigencia2Rect = CGRectMake(171, 300, 200, 35);
        UILabel *lblVigencia2 = [[UILabel alloc] initWithFrame:lblVigencia2Rect];
        [lblVigencia2 setBackgroundColor:[UIColor clearColor]];
        lblVigencia2.textAlignment = NSTextAlignmentLeft;
        [lblVigencia2 setFont:[UIFont fontWithName:@"ArialMT" size:12.0]];
        [subvista addSubview:lblVigencia2];
        NSMutableString *vigencia = [[NSMutableString alloc] init];
        if(tipoVehiculo.finalVigencia)
            [vigencia appendString:[self formatearFecha:tipoVehiculo.finalVigencia]];
        else
            [vigencia appendString:@"Indefinido"];
        lblVigencia2.text = vigencia;
        
        //Label Día (10)
        CGRect lblDiaRect = CGRectMake(33, 129, 84, 25);
        UILabel *lblDia = [[UILabel alloc] initWithFrame:lblDiaRect];
        lblDia.text = @"DÍA";
        [lblDia setBackgroundColor:[UIColor clearColor]];
        lblDia.textAlignment = NSTextAlignmentLeft;
        [lblDia setFont:[UIFont fontWithName:@"Arial-BoldMT" size:14.0]];
        [subvista addSubview:lblDia];
        
        //Label Fecha (11)
        CGRect lblFechaRect = CGRectMake(111, 129, 100, 25);
        UILabel *lblFecha = [[UILabel alloc] initWithFrame:lblFechaRect];
        lblFecha.text = @"FECHA";
        [lblFecha setBackgroundColor:[UIColor clearColor]];
        lblFecha.textAlignment = NSTextAlignmentCenter;
        [lblFecha setFont:[UIFont fontWithName:@"Arial-BoldMT" size:14.0]];
        [subvista addSubview:lblFecha];
        
        //Label Placas (12)
        CGRect lblPlacasRect = CGRectMake(207, 129, 84, 25);
        UILabel *lblPlacas = [[UILabel alloc] initWithFrame:lblPlacasRect];
        lblPlacas.text = @"PLACAS";
        [lblPlacas setBackgroundColor:[UIColor clearColor]];
        lblPlacas.textAlignment = NSTextAlignmentRight;
        [lblPlacas setFont:[UIFont fontWithName:@"Arial-BoldMT" size:14.0]];
        [subvista addSubview:lblPlacas];
        
        
        int posicionHoy = [self calcularPosicionDeHoy];
        BOOL termino = NO;
        int y=154;
        for (int i=0; i<7 && !termino; i++)
        {
            //Regla
            Regla *r = [tipoVehiculo.reglas objectAtIndex:posicionHoy];
            
            //Labels Días (13-19)
            CGRect lblDiaRect = CGRectMake(33, y, 84, 21);
            UILabel *labelDia = [[UILabel alloc] initWithFrame:lblDiaRect];
            [labelDia setBackgroundColor:[UIColor clearColor]];
            labelDia.textAlignment = NSTextAlignmentLeft;
            NSDateComponents *comp = [[NSCalendar currentCalendar] components:NSWeekdayCalendarUnit fromDate: r.fecha];
            labelDia.text = [self darDiaSemana:comp.weekday];
            labelDia.tag = i+1;
            labelDia.userInteractionEnabled = YES;
            UITapGestureRecognizer *singleTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(handleSingleTap:)];
            [labelDia addGestureRecognizer:singleTap];
            [subvista addSubview:labelDia];
            
            //Labels Fechas (20-26)
            CGRect lblFechaRect = CGRectMake(111, y, 100, 21);
            UILabel *labelFecha = [[UILabel alloc] initWithFrame:lblFechaRect];
            [labelFecha setBackgroundColor:[UIColor clearColor]];
            labelFecha.textAlignment = NSTextAlignmentCenter;
            labelFecha.text = [self formatearFecha:r.fecha];
            labelFecha.tag = i+1;
            singleTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(handleSingleTap:)];
            labelFecha.userInteractionEnabled = YES;
            [labelFecha addGestureRecognizer:singleTap];
            [subvista addSubview:labelFecha];
            
            //Labels placas (27-33)
            CGRect labelPlacasRect = CGRectMake(207, y, 84, 21);
            UILabel *labelPlacas = [[UILabel alloc] initWithFrame:labelPlacasRect];
            [labelPlacas setBackgroundColor:[UIColor clearColor]];
            labelPlacas.textAlignment = NSTextAlignmentRight;
            labelPlacas.text = r.numeros;
            labelPlacas.tag = i+1;
            singleTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(handleSingleTap:)];
            labelPlacas.userInteractionEnabled = YES;
            [labelPlacas addGestureRecognizer:singleTap];
            [subvista addSubview:labelPlacas];
            
            y+=21;        
            
            
            CGRect lblHorario1Rect;
            CGRect lblHorario2Rect;
            CGRect lblHorario3Rect;
            
            //Si es la primera fila
            if (i==0)
            {
                //Resaltar la primera fila
                labelDia.textColor = [UIColor whiteColor];
                [labelDia setFont:[UIFont fontWithName:@"Arial-BoldMT" size:16.0]];
                
                labelFecha.textColor = [UIColor whiteColor];
                [labelFecha setFont:[UIFont fontWithName:@"Arial-BoldMT" size:16.0]];
                
                labelPlacas.textColor = [UIColor whiteColor];
                [labelPlacas setFont:[UIFont fontWithName:@"Arial-BoldMT" size:16.0]];
                
                //Poner los horarios del día de hoy
                
                //Si la regla aplica hoy
                if ([r.horarios count]>0)
                {
                    //Si hay un sólo horario, se reubica el labelHorario1
                    if([r.horarios count]==1)
                    {
                        lblHorario1Rect = CGRectMake(95, 85, 210, 21);
                        lblHorario1.frame = lblHorario1Rect;
                        [lblHorario1 setFont:[UIFont fontWithName:@"Arial-BoldMT" size:20.0]];
                        
                        if(r.todoElDia)
                        {
                            UILabel *labelHorario = [[subvista subviews] objectAtIndex:5];
                            labelHorario.text = @"Todo el día";
                        }                           
                    }
                    
                    //Si hay dos horarios, se reubican labelHorario1 ylabelHorario2
                    else if([r.horarios count]==2)
                    {
                        lblHorario1Rect = CGRectMake(95, 75, 210, 21);
                        lblHorario1.frame = lblHorario1Rect;
                        [lblHorario1 setFont:[UIFont fontWithName:@"Arial-BoldMT" size:18.0]];
                        
                        lblHorario2Rect = CGRectMake(95, 95, 210, 21);
                        lblHorario2.frame = lblHorario2Rect;
                        [lblHorario2 setFont:[UIFont fontWithName:@"Arial-BoldMT" size:18.0]];
                    }
                    
                    //Si hay tres horarios, se reubican labelHorario1, labelHorario2 y labelHorario3
                    else if([r.horarios count]==3)
                    {
                        lblHorario1Rect = CGRectMake(95, 65, 210, 21);
                        lblHorario1.frame = lblHorario1Rect;
                        [lblHorario1 setFont:[UIFont fontWithName:@"Arial-BoldMT" size:18.0]];
                        
                        lblHorario2Rect = CGRectMake(90, 85, 210, 21);
                        [lblHorario1 setBackgroundColor:[UIColor clearColor]];
                        lblHorario2.frame = lblHorario2Rect;
                        [lblHorario2 setFont:[UIFont fontWithName:@"Arial-BoldMT" size:18.0]];
                        
                        lblHorario3Rect = CGRectMake(95, 105, 210, 21);
                        lblHorario3.frame = lblHorario3Rect;
                        [lblHorario3 setFont:[UIFont fontWithName:@"Arial-BoldMT" size:18.0]];
                    }
                    
                    for (int i=0; i<[r.horarios count] && !r.todoElDia; i++)
                    {
                        UILabel *labelHorario = [[subvista subviews] objectAtIndex:i+5];
                        NSMutableString *horarioString = [[NSMutableString alloc] init];
                        Horario *horario = [r.horarios objectAtIndex:i];
                        [horarioString appendString:[self formatearHora:horario.horaInicio]];
                        [horarioString appendString:@" a "];
                        [horarioString appendString:[self formatearHora:horario.horaFin]];
                        labelHorario.text = horarioString;
                    }
                }
                
                //Si la regla no aplica hoy
                else
                {
                    lblHorario1Rect = CGRectMake(95, 75, 210, 21);
                    lblHorario1.frame = lblHorario1Rect;
                    [lblHorario1 setFont:[UIFont fontWithName:@"Arial-BoldMT" size:20.0]];
                    lblHorario1.text = @"No hay pico y placa";
                    
                    //Si no es festivo, se reubica el labelHorario1
                    if(!r.esFestivo)
                    {
                        lblHorario1Rect = CGRectMake(95, 85, 210, 21);
                        lblHorario1.frame = lblHorario1Rect;
                    }
                    
                    //Si es festivo
                    else
                    {
                        lblHorario2Rect = CGRectMake(95, 100, 210, 21);
                        lblHorario2.frame = lblHorario2Rect;
                        [lblHorario2 setFont:[UIFont fontWithName:@"Arial-BoldMT" size:20.0]];
                        lblHorario2.text = @"Festivo";
                    }
                }
            }
            
            //Si es otra fila
            else
            {
                //Se pone el texto normal
                [labelDia setFont:[UIFont fontWithName:@"ArialMT" size:13.0]];
                [labelFecha setFont:[UIFont fontWithName:@"ArialMT" size:13.0]];
                [labelPlacas setFont:[UIFont fontWithName:@"ArialMT" size:13.0]];
            }
            
            //La siguiente regla no es nil
            @try
            {
                [tipoVehiculo.reglas objectAtIndex:posicionHoy+1];
                posicionHoy++;
            }
            @catch (NSException *exception)
            {
                termino=YES;
            }
        }
        
        //Label @pypcolombia
        CGRect lblPypRect = CGRectMake(-22, 59, 93, 11);
        UILabel *lblPyp = [[UILabel alloc] initWithFrame:lblPypRect];
        lblPyp.text = @"@pypcolombia";
        [lblPyp setBackgroundColor:[UIColor clearColor]];
        [lblPyp setTextColor:[UIColor blackColor]];
        [lblPyp setTransform:CGAffineTransformMakeRotation(-M_PI / 2)];
        lblPyp.textAlignment = NSTextAlignmentCenter;
        [lblPyp setFont:[UIFont fontWithName:@"Arial-BoldMT" size:11.0]];
        [subvista addSubview:lblPyp];
    }
    
    //No tiene vigencia
    else
    {
        imagen.image = [UIImage imageNamed:@"placa.png"];
        
        //Imagen de warning
        CGRect imagen4Rect = CGRectMake(121, 91, 80, 68);
        UIImageView *imagen4 = [[UIImageView alloc] initWithFrame:imagen4Rect];
        imagen4.image = [UIImage imageNamed:@"warning.png"];
        [subvista addSubview:imagen4];
        
        //Label Info desactualizada
        CGRect lblInfoRect = CGRectMake(28, 182, 264, 23);
        UILabel *lblInfo = [[UILabel alloc] initWithFrame:lblInfoRect];
        [lblInfo setBackgroundColor:[UIColor clearColor]];
        lblInfo.textAlignment = NSTextAlignmentCenter;
        [lblInfo setFont:[UIFont fontWithName:@"Arial-BoldMT" size:20.0]];
        lblInfo.text = @"Información desactualizada";
        [subvista addSubview:lblInfo];
        
        //Label Conéctese a internet
        CGRect lblConecteseRect = CGRectMake(72, 222, 188, 21);
        UILabel *lblConectese = [[UILabel alloc] initWithFrame:lblConecteseRect];
        [lblConectese setBackgroundColor:[UIColor clearColor]];
        lblConectese.textAlignment = NSTextAlignmentCenter;
        [lblConectese setFont:[UIFont fontWithName:@"ArialMT" size:13.0]];
        lblConectese.text = @"Por favor conéctate a internet";
        [subvista addSubview:lblConectese];
    }
    
    //Agregar subvista a vista
    [self.view addSubview:subvista];
}

-(void)pintarPlacaIPad
{
    UIView *subvista = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 768, 747)];
    
    //Imagen de fondo (0)
    CGRect imagenRect = CGRectMake(94, 10, 582, 724);
    UIImageView *imagen = [[UIImageView alloc] initWithFrame:imagenRect];    
    [subvista addSubview:imagen];
    
    //Tiene vigencia
    if([self calcularPosicionDeHoy] != -1)
    {
        //Imagen de fondo (0)
        imagen.image = [UIImage imageNamed:@"placa2.png"];
        
        //Label Ciudad (1)
        CGRect lblCiudadRect = CGRectMake(269, 51, 380, 60);
        UILabel *lblCiudad = [[UILabel alloc] initWithFrame:lblCiudadRect];
        lblCiudad.text = tipoVehiculo.ciudad.nombre;
        [lblCiudad setBackgroundColor:[UIColor clearColor]];
        lblCiudad.textAlignment = NSTextAlignmentCenter;
        [lblCiudad setFont:[UIFont fontWithName:@"Arial-BoldMT" size:54.0]];
        [subvista addSubview:lblCiudad];
        
        //Imagen del vehículo (2)
        CGRect imagen2Rect = CGRectMake(136, 94, 126, 102);
        UIImageView *imagen2 = [[UIImageView alloc] initWithFrame:imagen2Rect];
        [subvista addSubview:imagen2];
        
        //Label TipoVehículo (3)
        CGRect lblTipoRect = CGRectMake(136, 184, 126, 70);
        UILabel *lblTipo = [[UILabel alloc] initWithFrame:lblTipoRect];
        lblTipo.text = tipoVehiculo.tipo;
        lblTipo.numberOfLines = 2;
        [lblTipo setBackgroundColor:[UIColor clearColor]];
        lblTipo.textAlignment = NSTextAlignmentCenter;
        [lblTipo setFont:[UIFont fontWithName:@"Arial-BoldMT" size:24.0]];
        [subvista addSubview:lblTipo];
        
        //Imagen highlight (4)
        CGRect imagen3Rect = CGRectMake(106, 320, 556, 53);
        UIImageView *imagen3 = [[UIImageView alloc] initWithFrame:imagen3Rect];
        imagen3.image = [UIImage imageNamed:@"highlight.png"];
        [subvista addSubview:imagen3];
        
        //Label Horario 1 (5)
        UILabel *lblHorario1 = [[UILabel alloc] init];
        [lblHorario1 setBackgroundColor:[UIColor clearColor]];
        lblHorario1.textAlignment = NSTextAlignmentCenter;
        [subvista addSubview:lblHorario1];
        
        //Label Horario 2 (6)
        UILabel *lblHorario2 = [[UILabel alloc] init];
        [lblHorario2 setBackgroundColor:[UIColor clearColor]];
        lblHorario2.textAlignment = NSTextAlignmentCenter;
        [subvista addSubview:lblHorario2];
        
        //Label Horario 3 (7)
        UILabel *lblHorario3 = [[UILabel alloc] init];
        [lblHorario3 setBackgroundColor:[UIColor clearColor]];
        lblHorario3.textAlignment = NSTextAlignmentCenter;
        [subvista addSubview:lblHorario3];
        
        //Label Vigencia1 (8)
        CGRect lblVigencia1Rect = CGRectMake(238, 663, 175, 35);
        UILabel *lblVigencia1 = [[UILabel alloc] initWithFrame:lblVigencia1Rect];
        lblVigencia1.text = @"Vigente hasta:";
        [lblVigencia1 setBackgroundColor:[UIColor clearColor]];
        lblVigencia1.textAlignment = NSTextAlignmentLeft;
        [lblVigencia1 setFont:[UIFont fontWithName:@"Arial-BoldMT" size:24.0]];
        [subvista addSubview:lblVigencia1];
        
        //Label Vigencia2 (9)
        CGRect lblVigencia2Rect = CGRectMake(405, 663, 200, 35);
        UILabel *lblVigencia2 = [[UILabel alloc] initWithFrame:lblVigencia2Rect];
        [lblVigencia2 setBackgroundColor:[UIColor clearColor]];
        lblVigencia2.textAlignment = NSTextAlignmentLeft;
        [lblVigencia2 setFont:[UIFont fontWithName:@"ArialMT" size:24.0]];
        [subvista addSubview:lblVigencia2];
        
        //Label Día (10)
        CGRect lblDiaRect = CGRectMake(137, 286, 141, 35);
        UILabel *lblDia = [[UILabel alloc] initWithFrame:lblDiaRect];
        lblDia.text = @"DÍA";
        [lblDia setBackgroundColor:[UIColor clearColor]];
        lblDia.textAlignment = NSTextAlignmentLeft;
        [lblDia setFont:[UIFont fontWithName:@"Arial-BoldMT" size:28.0]];
        [subvista addSubview:lblDia];
        
        //Label Fecha (11)
        CGRect lblFechaRect = CGRectMake(293, 286, 184, 35);
        UILabel *lblFecha = [[UILabel alloc] initWithFrame:lblFechaRect];
        lblFecha.text = @"FECHA";
        [lblFecha setBackgroundColor:[UIColor clearColor]];
        lblFecha.textAlignment = NSTextAlignmentCenter;
        [lblFecha setFont:[UIFont fontWithName:@"Arial-BoldMT" size:28.0]];
        [subvista addSubview:lblFecha];
        
        //Label Placas (12)
        CGRect lblPlacasRect = CGRectMake(514, 286, 123, 35);
        UILabel *lblPlacas = [[UILabel alloc] initWithFrame:lblPlacasRect];
        lblPlacas.text = @"PLACAS";
        [lblPlacas setBackgroundColor:[UIColor clearColor]];
        lblPlacas.textAlignment = NSTextAlignmentRight;
        [lblPlacas setFont:[UIFont fontWithName:@"Arial-BoldMT" size:28.0]];
        [subvista addSubview:lblPlacas];
        
        //Imagen del vehículo
        if([tipoVehiculo.tipo isEqualToString:@"Particular"])
            imagen2.image = [UIImage imageNamed:@"car.png"];
        else if([tipoVehiculo.tipo rangeOfString:@"Moto"].location != NSNotFound)
            imagen2.image = [UIImage imageNamed:@"moto.png"];
        else if([tipoVehiculo.tipo isEqualToString:@"Taxi"])
            imagen2.image = [UIImage imageNamed:@"taxi.png"];
        else if([tipoVehiculo.tipo isEqualToString:@"Bus"])
            imagen2.image = [UIImage imageNamed:@"bus.png"];
        
        //Label Vigencia
        NSMutableString *vigencia = [[NSMutableString alloc] init];
        if(tipoVehiculo.finalVigencia)
            [vigencia appendString:[self formatearFecha:tipoVehiculo.finalVigencia]];
        else
            [vigencia appendString:@"Indefinido"];
        lblVigencia2.text = vigencia;
        
        int posicionHoy = [self calcularPosicionDeHoy];
        BOOL termino = NO;
        int y=327;
        for (int i=0; i<7 && !termino; i++)
        {
            //Regla
            Regla *r = [tipoVehiculo.reglas objectAtIndex:posicionHoy];
            
            //Labels Días (13-19)
            CGRect lblDiaRect = CGRectMake(137, y, 156, 46);
            UILabel *labelDia = [[UILabel alloc] initWithFrame:lblDiaRect];
            [labelDia setBackgroundColor:[UIColor clearColor]];
            labelDia.textAlignment = NSTextAlignmentLeft;
            NSDateComponents *comp = [[NSCalendar currentCalendar] components:NSWeekdayCalendarUnit fromDate: r.fecha];
            labelDia.text = [self darDiaSemana:comp.weekday];
            labelDia.tag = i+1;
            labelDia.userInteractionEnabled = YES;
            UITapGestureRecognizer *singleTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(handleSingleTap:)];
            [labelDia addGestureRecognizer:singleTap];
            [subvista addSubview:labelDia];
            
            //Labels Fechas (20-26)
            CGRect lblFechaRect = CGRectMake(293, y, 184, 46);
            UILabel *labelFecha = [[UILabel alloc] initWithFrame:lblFechaRect];
            [labelFecha setBackgroundColor:[UIColor clearColor]];
            labelFecha.textAlignment = NSTextAlignmentCenter;
            labelFecha.text = [self formatearFecha:r.fecha];
            labelFecha.tag = i+1;
            singleTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(handleSingleTap:)];
            labelFecha.userInteractionEnabled = YES;
            [labelFecha addGestureRecognizer:singleTap];
            [subvista addSubview:labelFecha];
            
            //Labels placas (27-33)
            CGRect labelPlacasRect = CGRectMake(477, y, 157, 46);
            UILabel *labelPlacas = [[UILabel alloc] initWithFrame:labelPlacasRect];
            [labelPlacas setBackgroundColor:[UIColor clearColor]];
            labelPlacas.textAlignment = NSTextAlignmentRight;
            labelPlacas.text = r.numeros;
            labelPlacas.tag = i+1;
            singleTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(handleSingleTap:)];
            labelPlacas.userInteractionEnabled = YES;
            [labelPlacas addGestureRecognizer:singleTap];
            [subvista addSubview:labelPlacas];
            
            y+=47;
            
            //Resaltar la primera fila
            if(i==0)
            {
                labelDia.textColor = [UIColor whiteColor];
                [labelDia setFont:[UIFont fontWithName:@"Arial-BoldMT" size:28.0]];
                
                labelFecha.textColor = [UIColor whiteColor];
                [labelFecha setFont:[UIFont fontWithName:@"Arial-BoldMT" size:28.0]];
                
                labelPlacas.textColor = [UIColor whiteColor];
                [labelPlacas setFont:[UIFont fontWithName:@"Arial-BoldMT" size:28.0]];
            }
            
            else
            {
                [labelDia setFont:[UIFont fontWithName:@"ArialMT" size:26.0]];
                [labelFecha setFont:[UIFont fontWithName:@"ArialMT" size:26.0]];
                [labelPlacas setFont:[UIFont fontWithName:@"ArialMT" size:26.0]];
            }
            
			CGRect lblHorario1Rect;
            CGRect lblHorario2Rect;
            CGRect lblHorario3Rect;
            //Poner los horarios del día de hoy
            if (i==0)
            {
                //Si la regla aplica hoy
                if ([r.horarios count]>0)
                {
                    //Si hay un sólo horario, se reubica el labelHorario1
                    if([r.horarios count]==1)
                    {
                        lblHorario1Rect = CGRectMake(269, 161, 380, 45);
                        lblHorario1.frame = lblHorario1Rect;
						[lblHorario1 setFont:[UIFont fontWithName:@"Arial-BoldMT" size:38.0]];
                        
                        if(r.todoElDia)
                        {
                            UILabel *labelHorario = [[subvista subviews] objectAtIndex:5];
                            labelHorario.text = @"Todo el día";
                        }
                    }
                    
                    //Si hay dos horarios, se reubican labelHorario1 y labelHorario2
                    else if([r.horarios count]==2)
                    {                    
                        lblHorario1Rect = CGRectMake(269, 136, 380, 45);
                        lblHorario1.frame = lblHorario1Rect;
						[lblHorario1 setFont:[UIFont fontWithName:@"Arial-BoldMT" size:38.0]];
                        
                        lblHorario2Rect = CGRectMake(269, 181, 380, 45);
                        lblHorario2.frame = lblHorario2Rect;
						[lblHorario2 setFont:[UIFont fontWithName:@"Arial-BoldMT" size:38.0]];
                    }
                    
                    //Si hay tres horarios, se reubican labelHorario1, labelHorario2 y labelHorario3
                    else if([r.horarios count]==3)
                    {
                        lblHorario1Rect = CGRectMake(281, 121, 380, 45);
                        lblHorario1.frame = lblHorario1Rect;
                        [lblHorario1 setFont:[UIFont fontWithName:@"Arial-BoldMT" size:38.0]];
                        
                        lblHorario2Rect = CGRectMake(269, 166, 380, 45);
                        lblHorario2.frame = lblHorario2Rect;
                        [lblHorario2 setFont:[UIFont fontWithName:@"Arial-BoldMT" size: 38.0]];
                        
                        lblHorario3Rect = CGRectMake(281, 211, 380, 45);
                        lblHorario3.frame = lblHorario3Rect;
                        [lblHorario3 setFont:[UIFont fontWithName:@"Arial-BoldMT" size: 38.0]];
                    }
                    
                    for (int i=0; i<[r.horarios count] && !r.todoElDia; i++)
                    {
                        UILabel *labelHorario = [[subvista subviews] objectAtIndex:i+5];
                        NSMutableString *horarioString = [[NSMutableString alloc] init];
                        Horario *horario = [r.horarios objectAtIndex:i];
                        [horarioString appendString:[self formatearHora:horario.horaInicio]];
                        [horarioString appendString:@" a "];
                        [horarioString appendString:[self formatearHora:horario.horaFin]];
                        labelHorario.text = horarioString;
                    }
                }
                
                //Si la regla no aplica hoy
                else
                {
					lblHorario1Rect = CGRectMake(279, 131, 380, 45);
                    lblHorario1.frame = lblHorario1Rect;
                    [lblHorario1 setFont:[UIFont fontWithName:@"Arial-BoldMT" size:38.0]];
                    lblHorario1.text = @"No hay pico y placa";
                    
                    
                    //Si no es festivo, se reubica el labelHorario1
                    if(!r.esFestivo)
                    {
                        lblHorario1Rect = CGRectMake(279, 161, 380, 45);
                        lblHorario1.frame = lblHorario1Rect;
                    }
                    
                    //Si es festivo
                    else
					{
						lblHorario2Rect = CGRectMake(279, 191, 380, 45);
                        lblHorario2.frame = lblHorario2Rect;
                        [lblHorario2 setFont:[UIFont fontWithName:@"Arial-BoldMT" size:38.0]];
						lblHorario2.text = @"Festivo";
					}
                }
            }
            
            //La siguiente regla no es nil
            @try
            {
                [tipoVehiculo.reglas objectAtIndex:posicionHoy+1];
                posicionHoy++;
            }
            @catch (NSException *exception)
            {
                termino=YES;
            }
        }
        
        //Label @pypcolombia
        CGRect lblPypRect = CGRectMake(25, 119, 196, 22);
        UILabel *lblPyp = [[UILabel alloc] initWithFrame:lblPypRect];
        lblPyp.text = @"@pypcolombia";
        [lblPyp setBackgroundColor:[UIColor clearColor]];
        [lblPyp setTextColor:[UIColor blackColor]];
        [lblPyp setTransform:CGAffineTransformMakeRotation(-M_PI / 2)];
        lblPyp.textAlignment = NSTextAlignmentCenter;
        [lblPyp setFont:[UIFont fontWithName:@"Arial-BoldMT" size:18.0]];
        [subvista addSubview:lblPyp];
    }
    
    //No tiene vigencia
    else
    {
        imagen.image = [UIImage imageNamed:@"placa.png"];
        
        //Imagen de warning
        CGRect imagen4Rect = CGRectMake(304, 186, 160, 136);
        UIImageView *imagen4 = [[UIImageView alloc] initWithFrame:imagen4Rect];
        imagen4.image = [UIImage imageNamed:@"warning.png"];
        [subvista addSubview:imagen4];
        
        //Label Info desactualizada
        CGRect lblInfoRect = CGRectMake(109, 340, 553, 38);
        UILabel *lblInfo = [[UILabel alloc] initWithFrame:lblInfoRect];
        [lblInfo setBackgroundColor:[UIColor clearColor]];
        lblInfo.textAlignment = NSTextAlignmentCenter;
        [lblInfo setFont:[UIFont fontWithName:@"Arial-BoldMT" size:36.0]];
        lblInfo.text = @"Información desactualizada";
        [subvista addSubview:lblInfo];
        
        //Label Conéctese a internet
        CGRect lblConecteseRect = CGRectMake(186, 442, 400, 35);
        UILabel *lblConectese = [[UILabel alloc] initWithFrame:lblConecteseRect];
        [lblConectese setBackgroundColor:[UIColor clearColor]];
        lblConectese.textAlignment = NSTextAlignmentCenter;
        [lblConectese setFont:[UIFont fontWithName:@"ArialMT" size:24.0]];
        lblConectese.text = @"Por favor conéctate a internet";
        [subvista addSubview:lblConectese];
    }
    
    //Agregar subvista a vista
    [self.view addSubview:subvista];
}

-(int) calcularPosicionDeHoy
{
    unsigned int flags = NSYearCalendarUnit | NSMonthCalendarUnit | NSDayCalendarUnit;
    NSCalendar* calendar = [NSCalendar currentCalendar];
    NSDateComponents* components = [calendar components:flags fromDate:[NSDate date]];
    NSDate* fechaBuscada = [calendar dateFromComponents:components];
    
    int posicionHoy =-1;
    int imin = 0;
    int imax = [tipoVehiculo.reglas count]-1;
    BOOL termino = NO;
    
    while (imax >= imin && !termino)
    {        
        int imed = (imin + imax) / 2;
        
        Regla *r = [tipoVehiculo.reglas objectAtIndex:imed];
        
        if ([r.fecha compare:fechaBuscada] == NSOrderedAscending)         
            imin = imed + 1;
        
        else if ([r.fecha compare:fechaBuscada] == NSOrderedDescending)
            imax = imed - 1;
        
        else
        {
            posicionHoy = imed;
            termino = YES;
        }       
    }
    
    return posicionHoy;
}

- (NSString*)formatearFecha:(NSDate*)fecha
{
    NSMutableString *fechaString = [[NSMutableString alloc] init];
    NSDateComponents *compFecha = [[NSCalendar currentCalendar] components:NSDayCalendarUnit | NSMonthCalendarUnit | NSYearCalendarUnit fromDate:fecha];
    
    if([[NSMutableString stringWithFormat:@"%i",compFecha.day] length]==1)
        [fechaString appendString:@"0"];
    
    [fechaString appendString:[NSMutableString stringWithFormat:@"%i",compFecha.day]];
    [fechaString appendString:@"-"];
    [fechaString appendString:[self darMes:compFecha.month]];
    [fechaString appendString:@"-"];
    [fechaString appendString:[NSMutableString stringWithFormat:@"%i",compFecha.year]];
    
    return fechaString;
}

- (NSString*)darDiaSemana:(int)dSemana
{
    NSString *stringDSemana = [[NSString alloc] init];
    if (dSemana == 1)
        stringDSemana = @"Domingo";
    else if (dSemana == 2)
        stringDSemana = @"Lunes";
    else if (dSemana == 3)
        stringDSemana = @"Martes";
    else if (dSemana == 4)
        stringDSemana = @"Miércoles";
    else if (dSemana == 5)
        stringDSemana = @"Jueves";
    else if (dSemana == 6)
        stringDSemana = @"Viernes";
    else if (dSemana == 7)
        stringDSemana = @"Sábado";
    return stringDSemana;
}

- (NSString*)darMes:(int)mes
{
    NSString *stringMes = [[NSString alloc] init];
    if (mes == 1)
        stringMes = @"Ene";
    else if (mes == 2)
        stringMes = @"Feb";
    else if (mes == 3)
        stringMes = @"Mar";
    else if (mes == 4)
        stringMes = @"Abr";
    else if (mes == 5)
        stringMes = @"May";
    else if (mes == 6)
        stringMes = @"Jun";
    else if (mes == 7)
        stringMes = @"Jul";
    else if (mes == 8)
        stringMes = @"Ago";
    else if (mes == 9)
        stringMes = @"Sep";
    else if (mes == 10)
        stringMes = @"Oct";
    else if (mes == 11)
        stringMes = @"Nov";
    else if (mes == 12)
        stringMes = @"Dic";
    return stringMes;
}

- (NSString*)formatearHora:(NSDate*)hora
{
    NSDateFormatter *formatterHora = [[NSDateFormatter alloc] init];
    [formatterHora setDateFormat:@"h:mma"];
    [formatterHora setPMSymbol:@"pm"];
    [formatterHora setAMSymbol:@"am"];
    NSString *horaString = [formatterHora stringFromDate:hora] ;
    
    return horaString;
}

- (void)handleSingleTap:(UITapGestureRecognizer *)recognizer
{
    int posicionHoy = [self calcularPosicionDeHoy];
    int touchedtag = recognizer.view.tag;
    posicionHoy+=(touchedtag-1);
    
    //Regla
    Regla *r = [tipoVehiculo.reglas objectAtIndex:posicionHoy];
    
    UIView * subvista = [self.view.subviews objectAtIndex:0];
    UILabel *lblHorario1 = [subvista.subviews objectAtIndex:5];
    UILabel *lblHorario2 = [subvista.subviews objectAtIndex:6];
    UILabel *lblHorario3 = [subvista.subviews objectAtIndex:7];
    UIImageView *imagenHighlight = [subvista.subviews objectAtIndex:4];
    
    lblHorario1.text = @"";
    lblHorario2.text = @"";
    lblHorario3.text = @"";
    
    CGRect lblHorario1Rect;
    CGRect lblHorario2Rect;
    CGRect lblHorario3Rect;
    //Si la regla aplica hoy
    if ([r.horarios count]>0)
    {
        //Si hay un sólo horario, se reubica el labelHorario1
        
        if([r.horarios count]==1)
        {
            //Es iPad
            if([[UIDevice currentDevice].model rangeOfString:@"iPad"].location != NSNotFound)
            {
                lblHorario1Rect = CGRectMake(269, 161, 380, 45);
                [lblHorario1 setFont:[UIFont fontWithName:@"Arial-BoldMT" size:38.0]];                
            }
            
            //Es iPhone
            else
            {
                lblHorario1Rect = CGRectMake(95, 85, 210, 21);
                [lblHorario1 setFont:[UIFont fontWithName:@"Arial-BoldMT" size:20.0]];
            }
            
            if(r.todoElDia)
            {
                UILabel *labelHorario = [[subvista subviews] objectAtIndex:5];
                labelHorario.text = @"Todo el día";
            }
            
            lblHorario1.frame = lblHorario1Rect;
        }
        
        //Si hay dos horarios, se reubican labelHorario1 ylabelHorario2
        else if([r.horarios count]==2)
        {
            //Es iPad
            if([[UIDevice currentDevice].model rangeOfString:@"iPad"].location != NSNotFound)
            {
                lblHorario1Rect = CGRectMake(269, 136, 380, 45);
                [lblHorario1 setFont:[UIFont fontWithName:@"Arial-BoldMT" size:38.0]];
                lblHorario2Rect = CGRectMake(269, 181, 380, 45);
                [lblHorario2 setFont:[UIFont fontWithName:@"Arial-BoldMT" size:38.0]];
            }
            
            //Es iPhone
            else
            {
                lblHorario1Rect = CGRectMake(95, 75, 210, 21);
                [lblHorario1 setFont:[UIFont fontWithName:@"Arial-BoldMT" size:18.0]];
                lblHorario2Rect = CGRectMake(95, 95, 210, 21);
                [lblHorario2 setFont:[UIFont fontWithName:@"Arial-BoldMT" size:18.0]];
            }
            
            lblHorario1.frame = lblHorario1Rect;
            lblHorario2.frame = lblHorario2Rect;
        }
        
        //Si hay 3 horarios, se dejan todos los label igual
        else
        {
            //Es iPad
            if([[UIDevice currentDevice].model rangeOfString:@"iPad"].location != NSNotFound)
            {
                lblHorario1Rect = CGRectMake(281, 121, 380, 45);
                [lblHorario1 setFont:[UIFont fontWithName:@"Arial-BoldMT" size:38.0]];
                
                lblHorario2Rect = CGRectMake(269, 166, 380, 45);
                [lblHorario2 setFont:[UIFont fontWithName:@"Arial-BoldMT" size: 38.0]];
                
                lblHorario3Rect = CGRectMake(281, 211, 380, 45);
                [lblHorario3 setFont:[UIFont fontWithName:@"Arial-BoldMT" size: 38.0]];
            }
            
            //Es iPhone
            else
            {
                lblHorario1Rect = CGRectMake(95, 65, 210, 21);
                [lblHorario1 setFont:[UIFont fontWithName:@"Arial-BoldMT" size:18.0]];
                lblHorario2Rect = CGRectMake(90, 85, 210, 21);
                [lblHorario2 setFont:[UIFont fontWithName:@"Arial-BoldMT" size:18.0]];
                lblHorario3Rect = CGRectMake(95, 105, 210, 21);
                [lblHorario3 setFont:[UIFont fontWithName:@"Arial-BoldMT" size:18.0]];
            }
            
            lblHorario1.frame = lblHorario1Rect;
            lblHorario2.frame = lblHorario2Rect;
            lblHorario3.frame = lblHorario3Rect;
        }
        
        for (int i=0; i<[r.horarios count] && !r.todoElDia; i++)
        {
            UILabel *labelHorario = [[subvista subviews] objectAtIndex:i+5];
            NSMutableString *horarioString = [[NSMutableString alloc] init];
            Horario *horario = [r.horarios objectAtIndex:i];
            [horarioString appendString:[self formatearHora:horario.horaInicio]];
            [horarioString appendString:@" a "];
            [horarioString appendString:[self formatearHora:horario.horaFin]];
            labelHorario.text = horarioString;
        }
    }
    
    //Si la regla no aplica hoy
    else
    {
        lblHorario1.text = @"No hay pico y placa";
        
        //Es iPad
        if([[UIDevice currentDevice].model rangeOfString:@"iPad"].location != NSNotFound)
        {
            lblHorario1Rect = CGRectMake(279, 131, 380, 45);
            [lblHorario1 setFont:[UIFont fontWithName:@"Arial-BoldMT" size:38.0]];
        }
        
        //Es iPhone
        else
        {
            lblHorario1Rect = CGRectMake(95, 75, 210, 21);
            [lblHorario1 setFont:[UIFont fontWithName:@"Arial-BoldMT" size:20.0]];
        }
        
        lblHorario1.frame = lblHorario1Rect;
        
        //Si no es festivo, se reubica el labelHorario1
        if(!r.esFestivo)
        {
            //Es iPad
            if([[UIDevice currentDevice].model rangeOfString:@"iPad"].location != NSNotFound)
                lblHorario1Rect = CGRectMake(279, 161, 380, 45);
            
            //Es iPhone
            else
                lblHorario1Rect = CGRectMake(95, 85, 210, 21);
            
            lblHorario1.frame = lblHorario1Rect;
        }
        
        //Si es festivo, se deja labelHorario1 igual
        else
        {
            lblHorario2.text = @"Festivo";
            
            //Es iPad
            if([[UIDevice currentDevice].model rangeOfString:@"iPad"].location != NSNotFound)
            {
                lblHorario2Rect = CGRectMake(279, 191, 380, 45);
                [lblHorario2 setFont:[UIFont fontWithName:@"Arial-BoldMT" size:38.0]];
            }
            
            //Es iPhone
            else
            {
                lblHorario2Rect = CGRectMake(95, 100, 210, 21);
                [lblHorario2 setFont:[UIFont fontWithName:@"Arial-BoldMT" size:20.0]];
            }
            
            lblHorario2.frame = lblHorario2Rect;
        }
    }
    
    for (UILabel * label in subvista.subviews)
    {
        //Highlight
        if((label.tag) == touchedtag)
        {
            CGRect imagenRect;
            
            //Es iPad
            if([[UIDevice currentDevice].model rangeOfString:@"iPad"].location != NSNotFound)
            {
                [label setFont:[UIFont fontWithName:@"Arial-BoldMT" size:28.0]];
                imagenRect = CGRectMake(106, label.frame.origin.y-2, 556, 48);
            }
            
            //Es iPhone
            else
            {
                [label setFont:[UIFont fontWithName:@"Arial-BoldMT" size:16.0]];
                imagenRect = CGRectMake(14, label.frame.origin.y-2, 293, 23);
            }
            
            imagenHighlight.frame = imagenRect;
            label.textColor = [UIColor whiteColor];
        }
        
        //Se vuelven a la normalidad el resto de filas
        else if (label.tag != 0)
        {
            label.textColor = [UIColor blackColor];
            
            //Es iPad
            if([[UIDevice currentDevice].model rangeOfString:@"iPad"].location != NSNotFound)
                [label setFont:[UIFont fontWithName:@"ArialMT" size:26.0]];
            
            //Es iPhone
            else
                [label setFont:[UIFont fontWithName:@"ArialMT" size:13.0]];
        }
    }
}

@end
